package com.frkj.yuan.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.frkj.yuan.R;
import com.frkj.yuan.adapter.BaseRecyclerHolder;
import com.frkj.yuan.adapter.BaseRecyclerRefreshAdapter;
import com.frkj.yuan.api.bean.HospitalBean;
import com.frkj.yuan.api.category.HospitalListCategory;
import com.frkj.yuan.api.presenter.HospitalListPresenter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.dialog.LoginOutDialog;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;
import com.frkj.yuan.view.RecyclerViewLayoutManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 信息说明: 医院
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/3/29 20 ：10
 * 项目名称：yuanban
 */
public class HospitalListActivity extends BaseActivity implements HospitalListCategory.View, TextWatcher {
    @BindView(R.id.edit_hosptial)
    EditText mEditText;
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.base_recycler)
    RecyclerView mRecyclerView;
    private HospitalListPresenter mPresenter;
    private BaseRecyclerRefreshAdapter<HospitalBean.HospitalBeanData.HospitalBeanDataList> mAdapter;
    private int pageNumber = 1;
    private String mStateTyped;

    @OnClick({R.id.base_back,R.id.edit_hosptial})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.base_back:
                finishWitchAnimation();
                break;
        }
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_hospital_view;
    }
    @Override
    public void initLayoutView() {
        mEditText.addTextChangedListener(this);
        mStateTyped = getIntent().getStringExtra("yiyuan");
        FullScreenUtils.getInstance().fullScreen(HospitalListActivity.this,true);
        mTitleMsg.setText("服务医院");
        mPresenter = new HospitalListPresenter(this);
        SetPersenter(mPresenter);
        JSONObject result = new JSONObject();
        try {
            result.put("provCode", 620000);
            result.put("cityCode", 620100);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mPresenter.getHospitalList(pageNumber,result.toString());
        RecyclerViewLayoutManager.setLinearVerticalItemDecorationManager(HospitalListActivity.this, mRecyclerView);
    }
    @Override
    public void getHospitalList(HospitalBean hospitalBean) {
        if (hospitalBean.getCode()== ConstantStaticUrl.SUCCESS_CODE){
           mAdapter =  new BaseRecyclerRefreshAdapter<HospitalBean.HospitalBeanData.HospitalBeanDataList>(HospitalListActivity.this, hospitalBean.getData().getList(), R.layout.hostp_item_view) {
                @Override
                public void convert(BaseRecyclerHolder holder, HospitalBean.HospitalBeanData.HospitalBeanDataList item, int position, boolean isScrolling) throws IOException {
                    holder.setText(R.id.yiyuan_name, item.getName());
                }
            };
            mRecyclerView.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
//                    if (mStateTyped!=null&&mStateTyped.equals("yiyuan")){
                        //数据是使用Intent返回
                        Intent intent = new Intent();
                        //把返回数据存入Intent
                        intent.putExtra("yiyuan",hospitalBean.getData().getList().get(position).getName() );
                        intent.putExtra("hospitalId",hospitalBean.getData().getList().get(position).getId());
                        //设置返回数据
                        HospitalListActivity.this.setResult(400, intent);//RESULT_OK为自定义常量
                        //关闭Activity
                        HospitalListActivity.this.finish();
//                    }else {
//                        Bundle bundle = new Bundle();
//                        bundle.putLong("hospitalId",hospitalBean.getData().getList().get(position).getId());
//                        openActivityWitchAnimation(YuyueKeShiActivity.class,bundle);
//                        //关闭Activity
//                        HospitalListActivity.this.finish();
                }
            });
        }else if (hospitalBean.getCode()==401){
            LoginOutDialog dialog = new LoginOutDialog(HospitalListActivity.this);
            dialog.show();
            dialog.findViewById(R.id.circler_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.findViewById(R.id.to_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityWitchAnimation(UserLoginActivity.class);
                    SPUtils.clear(HospitalListActivity.this);
                    HospitalListActivity.this.finish();
                    dialog.dismiss();
                }
            });
        }else {
            ToastUtil.showToastINFO(HospitalListActivity.this,hospitalBean.getMsg());
        }
    }
    @Override
    public void SetPersenter(Object persenter) {

    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(HospitalListActivity.this,mErrorMsg);
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
